package gold.digger;

import gold.vo.ListNode;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC61 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public ListNode rotateRight(ListNode head, int k) {
            if (k == 0 || null == head || head.next == null) return head;

            int len = 0;
            ListNode fast = head, slow = head, tail = head;
            while (fast != null) {
                ++len;
                tail = fast;
                fast = fast.next;
            }

            if (k % len == 0) return head;
            k = len - k % len - 1;
            while (k-- >= 0) {
                fast = slow;
                slow = slow.next;
            }

            fast.next = null;
            fast = head;
            head = slow;
            tail.next = fast;

            return head;
        }
    }

    public void run() {
        Solution solution = new Solution();
        ListNode head = ListNode.createList("1->2");
        System.out.println(solution.rotateRight(head, 2));
    }

    public static void main(String[] args) throws Exception {
        LC61 an = new LC61();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
